package com.example.playmusic;

import java.util.ArrayList;
import java.util.List;

import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.app.Activity;
import android.app.DownloadManager.Query;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

public class PlayMusicBrowser extends Activity {
	private Cursor mCursor;
	private int count;
	private String tag = "21106";
	private List<ObjectSong> list;
	private ListView listView;
	private MediaPlayer mp = new MediaPlayer();
	private ImageButton btPlay;
	private TextView txtTimePlay;
	private TextView txtTimeRemain;
	private Thread thread;

	private ProgressBar bar;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_play_music_browser);

		SetListSong();
		btPlay = (ImageButton) findViewById(R.id.button_play);
		listView = (ListView) findViewById(R.id.list_music);
		ArrayAdapterList adapList = new ArrayAdapterList(this, list);
		listView.setAdapter(adapList);
		txtTimePlay = (TextView) findViewById(R.id.time_play);
		txtTimeRemain = (TextView) findViewById(R.id.time_all);
		bar = (ProgressBar) findViewById(R.id.seekbar_run_play);
		listView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> listadapter, View item,
					int pos, long id) {
				if (mp.isPlaying()) {
					HandlerTime((list.get(pos).getTime()));
				} else {
					HandlerTime((list.get(pos).getTime()));
				}
				play(list.get(pos).getParth());
				// txtTimePlay.setText(changeTime(list.get(pos).getTime())+"");
				// Log.i(tag, changeTime(list.get(pos).getTime())+"");

			}
		});
		btPlay.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				pause();

			}
		});

	}

	public void SetListSong() {
		String[] cols = { MediaStore.Audio.Media._ID,
				MediaStore.Audio.Media.DATA, MediaStore.Audio.Media.DURATION,
				MediaStore.Audio.Media.SIZE, MediaStore.Audio.Media.TITLE };
		mCursor = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
				cols, null, null, null);
		count = mCursor.getCount();
		SetList(count, cols, mCursor);
	}

	public void SetList(int stlength, String[] columns, Cursor cursor) {
		cursor.moveToFirst();
		list = new ArrayList<ObjectSong>();
		while (cursor.getPosition() < stlength) {

			ObjectSong obj = new ObjectSong(
					cursor.getString(cursor
							.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA)),
					cursor.getString(cursor
							.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE)),
					cursor.getString(cursor
							.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE)),
					cursor.getLong(cursor
							.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION)));
			list.add(obj);
			cursor.moveToNext();
		}
	}

	public void play(String Parth) {
		if (mp.isPlaying()) {
			mp.reset();

		}
		try {
			mp.setDataSource(Parth);
			mp.prepare();
			mp.start();

		} catch (Exception e) {

		}

	}

	public void pause() {
		if (mp.isPlaying()) {
			mp.pause();
		} else {
			mp.start();

		}
	}

	public void HandlerTime(long time) {
		Log.i(tag, "go");
		final int timesec = (int) time / 1000;
		Runnable runnable = new Runnable() {
			@Override
			public void run() {
				RunTime(timesec);

			}
		};

		thread = new Thread(runnable);
		thread.start();
	}

	public void RunTime(int timesec) {
		for (int i = 0; i < timesec; i++) {
			doFakeWork();
			txtTimePlay.setText(i / 60 + " : " + i % 60);
			txtTimeRemain.setText((timesec - i) / 60 + " : " + (timesec - i)
					% 60);
			Log.i(tag, i + "");
		}
	}

	private void doFakeWork() {
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}
